#!/bin/csh -f
#
# AL 15/9/99
#
# Wrapper around cvs/svn.


if ( ! -d CVS ) then
    svn $*
    exit $status
endif

if ( "$1" == ed ) then
    shift
    dvs_ed $*
    exit $status
endif

set get_answer='echo -n ">> ";set answer=$<;echo ""'

if ( ! $?DGB_CVS_DIR ) then
    setenv DGB_CVS_DIR /users/appman/dvs
    if ( $?WORK ) then
	if ( -e $WORK/dvs ) setenv DGB_CVS_DIR $WORK/dvs
    endif
endif
source $DGB_CVS_DIR/set_dvs_env.csh

if ( ! $?OD_WORKDIR ) then
    set oddir="od"
else
    set oddir=`echo $OD_WORKDIR`
endif

set dgbdir=`echo $oddir | sed 's/od/dgb/'`
set curcvsroot=`echo $CVSROOT`

if ( -e ./CVS/Repository ) then
    set curcvsroot=`cat ./CVS/Root`
    set curcvsrepos=`cat ./CVS/Repository`
    set currel=$curcvsrepos:h:h:h:h:h:h
    if ( $currel != $oddir && $currel != $dgbdir ) then
	echo "The release you're working in differs from OD_WORKDIR"
	echo "./CVS/Repository : $currel"
	echo "OD_WORKDIR       : $oddir"
	exit 1
    endif
endif

set cvsremoteargs=""
if ( `echo $curcvsroot | grep $DGB_CVS_HOST` == "" ) then
    set cvsremoteargs="-z3"
endif

if ( $#argv > 0 ) then
    if ( "$1" == add ) then
	shift
	set addsilent="no"
	if ( "$1" == "--addsilent" ) then
	    set addsilent="yes"
	    shift
	endif
	foreach fil ( $* )
	    set type = `file $fil | awk '{print $2,$3,$4,$5,$6,$7,$8}'`

	    set text="no"
	    if( `echo $type |grep text` != "" )  set text="yes"
	    if( `echo $type |grep "(Ascii)"` != "" )  set text="yes"
	    if( `echo $type |grep directory` != "" )  set text="yes"

	    set cvsargs=""
	    if ( $text == "no" ) then
		if( $addsilent == "no" ) then
		    echo $fil "seems to be a binary file"
		    echo "Use dvs add --addsilent to automatically add binaries"
		    echo "Do you want to add it as a binary file? (Y/n)"

		    eval $get_answer
		else
		    echo $fil "seems to be a binary file"
		    set answer="Y"
		endif
		if ( $answer != "n" && $answer != "N" ) then
		    echo "adding as binary"
		    set cvsargs="-kb"
		else
		    echo "adding as text"
		    if ( $cvsremoteargs != "" ) then
			# no keyword substitution
			set cvsargs="-ko"
		    endif
		endif
	    endif
	    cvs add $cvsargs $fil
	end

	exit $status
    endif

    if ( "$1" == update ) then
	shift
	cvs $cvsremoteargs -q update -d -P $*
	exit $status
    endif

    if ( "$1" == commit ) then
	shift
	foreach fil ( $* )
	    set isascii=`file $fil | grep ASCII | wc -l`
	    if ( -x $fil && $isascii == 1 ) then
		set answer=N
		echo "File $fil is ASCII and executable."
		echo "Do you want to commit as executabe? (y/N)"
		eval $get_answer
		if ( "$answer" != "y" && "$answer" != "Y" ) then
		    exit $status
		endif
	    endif
	end
	cvs commit $*
	exit $status
    endif

endif
cvs $cvsremoteargs $*

